<template>
  <header class="admin-header">
    <div class="header-left">
      <h2 class="admin-title">校园信息平台 - 管理中心</h2>
    </div>
    <div class="header-right">
      <el-dropdown>
        <span class="admin-user">
          <el-avatar :size="32" :src="userAvatar" />
          <span class="user-name">{{ userInfo.nickname || userInfo.username }}</span>
          <el-icon><arrow-down /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="goToHome">
              <el-icon><copy-document /></el-icon>返回前台
            </el-dropdown-item>
            <el-dropdown-item @click="goToProfile">
              <el-icon><user /></el-icon>个人资料
            </el-dropdown-item>
            <el-dropdown-item divided @click="handleLogout">
              <el-icon><switch-button /></el-icon>退出登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </header>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import useUserInfoStore from '@/stores/userInfo'
import { useTokenStore } from '@/stores/token'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  ArrowDown,
  CopyDocument,
  User,
  SwitchButton
} from '@element-plus/icons-vue'

const router = useRouter()
const userInfoStore = useUserInfoStore()
const tokenStore = useTokenStore()

const userInfo = computed(() => userInfoStore.info || {})
const userAvatar = computed(() => userInfo.value.avatar || '')

const goToHome = () => {
  router.push('/')
}

const goToProfile = () => {
  router.push('/profile')
}

// 退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    // 清除token和用户信息
    tokenStore.removeToken()
    userInfoStore.removeInfo()
    // 清除localStorage数据
    localStorage.removeItem('token')
    localStorage.removeItem('userInfo')
    
    // // 获取当前标签页ID
    // const tabId = sessionStorage.getItem('tabId')
    // if (tabId) {
    //   // 清除当前标签页的localStorage数据
    //   localStorage.removeItem(`token-${tabId}`)
    // }
    
    // // 清除所有localStorage中的token数据（为了确保完全清除）
    // Object.keys(localStorage).forEach(key => {
    //   if (key.startsWith('token-')) {
    //     localStorage.removeItem(key)
    //   }
    // })
    
    // // 清除tabId
    // sessionStorage.removeItem('tabId')
    
    ElMessage.success('退出成功')
    router.push('/login')
  } catch {
    ElMessage.error('退出失败')
  }
}
</script>

<style scoped>
.admin-header {
  height: 60px;
  background: #ffffff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.header-left .admin-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.admin-user {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background-color 0.3s;
}

.admin-user:hover {
  background-color: #f5f7fa;
}

.user-name {
  font-size: 14px;
  color: #303133;
}
</style>